package com.imooc.spark.streaming

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * http://spark.apache.org/docs/latest/streaming-programming-guide.html
  *
  * @description 使用Spark Streaming处理文件系统（local/hdfs）的数据
  * @author yuyon26@126.com
  * @date 2018/10/22 0:02
  */
object FileWordCount {

  def main(args: Array[String]): Unit = {

    val inputDir = "D:\\spark\\spark_streaming"

    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("FileWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(5))

    val lines = ssc.textFileStream(inputDir)

    val result = lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)

    result.print()

    ssc.start()
    ssc.awaitTermination()

  }


}
